<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <link rel="stylesheet" href="layui/css/layui.css" media="all">
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>注册</legend>
</fieldset>
<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">账号</label>
        <div class="layui-input-block">
            <input type="text" name="userName" id="userName" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-inline">
            <input type="password" name="password" id="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">学号</label>
        <div class="layui-input-block">
            <input type="text" name="stuNo" id="stuNo" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-inline">
            <input type="text" name="stuName" id="stuName" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" id="register" lay-submit lay-filter="formDemo">注册</button>
        </div>
    </div>
</form>

<script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.3.1/jquery.min.js"></script>
<script src="layui/layui.js"></script>
<script>

    $("#register").on("click",function () {
        $.ajax({
            url:"/studentadminsystem/RegisterUserServlet2"
            ,type:"post"
            ,async:false
            ,cache: true
            ,data:{
                userName:$("#userName").val()
                ,password:$("#password").val()
            }
            ,success:function (data) {
                //判断是否为json格式
                if((typeof data=='object')&&data.constructor==Object){
                    obj=data;
                }else{
                    obj  = eval("("+data+")");
                }
                //alert(obj.result);
                if(obj.result=="ok")
                    RegisterStudent();
                else
                    alert("用户冲突")
            }
        });
    })


    function RegisterStudent(){
        $.ajax({
            url:"/studentadminsystem/RegisterStudentServlet2"
            ,type:"post"
            ,async:false
            ,cache: true
            ,data:{
                stuNo:$("#stuNo").val()
                ,stuName:$("#stuName").val()
                ,userName:$("#userName").val()
            }
            ,success:function (data) {
                //判断是否为json格式
                if((typeof data=='object')&&data.constructor==Object){
                    obj=data;
                }else{
                    obj  = eval("("+data+")");
                }
                //alert(obj.result);
                if(obj.result=="ok")
                    alert("注册成功")
                else
                    alert("学号重复")
            }
        });
    }

    //上传照片
    layui.use('upload', function(){
        var upload = layui.upload;

        //执行实例
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            ,url: '/upload/' //上传接口
            ,done: function(res){
                //上传完毕回调
            }
            ,error: function(){
                //请求异常回调
            }
        });
    });

</script>
</body>
</html>